﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>TrafficLight: TrafficLight.cs Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                C# examples using state machines generated by StateBuilderDotNet
            </h1>
            <h3>
                TrafficLight: TrafficLight.cs Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">TrafficLight.cs</div>  </div>
</div>
<div class="contents">
<a href="_traffic_light_8cs.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 ﻿
<a name="l00002"></a>00002 <span class="keyword">namespace </span>StateForge.Examples.TrafficLight
<a name="l00003"></a>00003 {
<a name="l00004"></a>00004     <span class="keyword">using</span> System;
<a name="l00005"></a>00005     <span class="keyword">using</span> StateForge.StateMachine;
<a name="l00006"></a>00006     <span class="keyword">using</span> System.Diagnostics;
<a name="l00007"></a>00007     <span class="keyword">using</span> System.Threading;
<a name="l00008"></a>00008 
<a name="l00012"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html">00012</a>     <span class="keyword">public</span> <span class="keyword">class </span><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html" title="The Light class knows how to switch on and off the green, yellow and red lights.">Light</a>
<a name="l00013"></a>00013     {
<a name="l00017"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#aa813615d941d7db34533f1e9f301990b">00017</a>         <span class="keyword">public</span> <span class="keywordtype">long</span> TimerRedDuration { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00018"></a>00018 
<a name="l00022"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#af04ebbcbfec99e018181db290aa39406">00022</a>         <span class="keyword">public</span> <span class="keywordtype">long</span> TimerYellowDuration { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00023"></a>00023 
<a name="l00027"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#abc1f28d35864f18919a8236da51a06fc">00027</a>         <span class="keyword">public</span> <span class="keywordtype">long</span> TimerGreenDuration { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00028"></a>00028 
<a name="l00032"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#a42bf05777b6870d6ebd3128af130fae9">00032</a>         <span class="keyword">public</span> <span class="keywordtype">long</span> TimerMaxDuration { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00033"></a>00033 
<a name="l00034"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#abf0d21972ee5013cde3c36e7c20c3a0f">00034</a>         <span class="keyword">public</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html" title="The Light class knows how to switch on and off the green, yellow and red lights.">Light</a>()
<a name="l00035"></a>00035         {
<a name="l00036"></a>00036             TimerRedDuration = 500; <span class="comment">/* msec */</span>
<a name="l00037"></a>00037             TimerYellowDuration = 500;<span class="comment">/* msec */</span>
<a name="l00038"></a>00038             TimerGreenDuration = 500;<span class="comment">/* msec */</span>
<a name="l00039"></a>00039 
<a name="l00040"></a>00040             TimerMaxDuration = 1900; <span class="comment">/* msec */</span>
<a name="l00041"></a>00041         }
<a name="l00042"></a>00042 
<a name="l00043"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#aee97c76dadbdb4737afc1f12f4193362">00043</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> TurnOnGreen()
<a name="l00044"></a>00044         {
<a name="l00045"></a>00045             Console.WriteLine(<span class="stringliteral">&quot;TurnOnGreen&quot;</span>);
<a name="l00046"></a>00046         }
<a name="l00047"></a>00047 
<a name="l00048"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#a06d63e0a224594a779ce677b3bda2b12">00048</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> TurnOffGreen()
<a name="l00049"></a>00049         {
<a name="l00050"></a>00050             Console.WriteLine(<span class="stringliteral">&quot;TurnOffGreen&quot;</span>);
<a name="l00051"></a>00051         }
<a name="l00052"></a>00052 
<a name="l00053"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#ab8948bf092d181992469568ae5f5626b">00053</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> TurnOnYellow()
<a name="l00054"></a>00054         {
<a name="l00055"></a>00055             Console.WriteLine(<span class="stringliteral">&quot;TurnOnYellow&quot;</span>);
<a name="l00056"></a>00056         }
<a name="l00057"></a>00057 
<a name="l00058"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#abe4b68d38f6c669c4d71262211bc0b4f">00058</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> TurnOffYellow()
<a name="l00059"></a>00059         {
<a name="l00060"></a>00060             Console.WriteLine(<span class="stringliteral">&quot;TurnOffYellow&quot;</span>);
<a name="l00061"></a>00061         }
<a name="l00062"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#a08ebe49adfb8e1dac642178bd1ff254c">00062</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> TurnOnRed()
<a name="l00063"></a>00063         {
<a name="l00064"></a>00064             Console.WriteLine(<span class="stringliteral">&quot;TurnOnRed&quot;</span>);
<a name="l00065"></a>00065         }
<a name="l00066"></a>00066 
<a name="l00067"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html#a1090fdb9d497f6ecfe6ab46c9333d0bf">00067</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> TurnOffRed()
<a name="l00068"></a>00068         {
<a name="l00069"></a>00069             Console.WriteLine(<span class="stringliteral">&quot;TurnOffRed&quot;</span>);
<a name="l00070"></a>00070         }
<a name="l00071"></a>00071     }
<a name="l00072"></a>00072 
<a name="l00076"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html">00076</a>     <span class="keyword">public</span> <span class="keyword">partial class </span><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html" title="TrafficLight is a container class which hold instances of a TrafficLightActuator and a generated Traf...">TrafficLight</a>
<a name="l00077"></a>00077     {
<a name="l00078"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#ae8acc5adf832db77605b4ca0a581ecec">00078</a>         <span class="keyword">private</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html" title="The Light class knows how to switch on and off the green, yellow and red lights.">Light</a> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#ae8acc5adf832db77605b4ca0a581ecec">light</a>;
<a name="l00079"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#aa193cbc9ee91c3fdaab62721279912ff">00079</a>         <span class="keyword">private</span> TrafficLightContext <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#aa193cbc9ee91c3fdaab62721279912ff">context</a>;
<a name="l00080"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#a0d98f8e228c54d9f24f1385b6a43c171">00080</a>         <span class="keyword">private</span> <span class="keyword">static</span> readonly <span class="keywordtype">string</span> traceName = <span class="stringliteral">&quot;TrafficLight&quot;</span>;
<a name="l00081"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#a24f149c0a56e0dd08e3d990cde18e038">00081</a>         <span class="keyword">private</span> <span class="keyword">static</span> TraceSource ts = <span class="keyword">new</span> TraceSource(traceName);
<a name="l00082"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#aab20f486ca9f003e7b92ce13824f53d0">00082</a>         <span class="keyword">private</span> <span class="keyword">static</span> AutoResetEvent autoResetEvent = <span class="keyword">new</span> AutoResetEvent(<span class="keyword">false</span>);
<a name="l00083"></a>00083 
<a name="l00084"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#a92c91550251282b86202d19cfb655df3">00084</a>         <span class="keyword">public</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html" title="TrafficLight is a container class which hold instances of a TrafficLightActuator and a generated Traf...">TrafficLight</a>()
<a name="l00085"></a>00085         {
<a name="l00086"></a>00086             this.light = <span class="keyword">new</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_light.html" title="The Light class knows how to switch on and off the green, yellow and red lights.">Light</a>();
<a name="l00087"></a>00087             this.context = <span class="keyword">new</span> TrafficLightContext(light);
<a name="l00088"></a>00088             this.context.Observer = ObserverTrace.Instance(traceName);
<a name="l00089"></a>00089             this.context.EndHandler += <span class="keyword">new</span> EventHandler&lt;EventArgs&gt;(StateMachineEnd);
<a name="l00090"></a>00090             this.context.EnterInitialState();
<a name="l00091"></a>00091         }
<a name="l00092"></a>00092 
<a name="l00093"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#a1235447b540d56802f1e854d9e22c0b2">00093</a>         <span class="keyword">private</span> <span class="keywordtype">void</span> StateMachineEnd(<span class="keywordtype">object</span> sender, EventArgs e)
<a name="l00094"></a>00094         {
<a name="l00095"></a>00095             ts.TraceInformation(<span class="stringliteral">&quot;state machine has ended&quot;</span>);
<a name="l00096"></a>00096             autoResetEvent.Set();
<a name="l00097"></a>00097         }
<a name="l00098"></a>00098 
<a name="l00099"></a><a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html#a777384d94d2ebe441761a02efcedc780">00099</a>         <span class="keyword">static</span> <span class="keywordtype">void</span> Main(<span class="keywordtype">string</span>[] args)
<a name="l00100"></a>00100         {
<a name="l00101"></a>00101             <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html" title="TrafficLight is a container class which hold instances of a TrafficLightActuator and a generated Traf...">TrafficLight</a> myTrafficLight = <span class="keyword">new</span> <a class="code" href="class_state_forge_1_1_examples_1_1_traffic_light_1_1_traffic_light.html" title="TrafficLight is a container class which hold instances of a TrafficLightActuator and a generated Traf...">TrafficLight</a>();
<a name="l00102"></a>00102             myTrafficLight.Start(<span class="stringliteral">&quot;Ciao&quot;</span>);
<a name="l00103"></a>00103             autoResetEvent.WaitOne();
<a name="l00104"></a>00104             ts.TraceInformation(<span class="stringliteral">&quot;TrafficLight has ended&quot;</span>);
<a name="l00105"></a>00105             Environment.Exit(0);
<a name="l00106"></a>00106         }
<a name="l00107"></a>00107     }
<a name="l00108"></a>00108 }
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
